package com.easyjob.service.impl;

import com.easyjob.entity.enums.PageSize;
import com.easyjob.entity.po.Category;
import com.easyjob.entity.po.ExamQuestion;
import com.easyjob.entity.po.QuestionBank;
import com.easyjob.entity.po.QuestionInfo;
import com.easyjob.entity.query.*;
import com.easyjob.entity.vo.PaginationResultVO;
import com.easyjob.exception.BusinessException;
import com.easyjob.mappers.CategoryMapper;
import com.easyjob.mappers.ExamQuestionMapper;
import com.easyjob.mappers.QuestionBankMapper;
import com.easyjob.mappers.QuestionInfoMapper;
import com.easyjob.service.QuestionBankService;
import com.easyjob.utils.StringTools;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Const
 * @version 1.0
 * @email 1357042069@qq.com
 * @date 2023/12/14
 */
@Service("questionBankService")
public class QuestionBankServiceImpl implements QuestionBankService {
    @Resource
    private QuestionBankMapper<QuestionBank, QuestionBankQuery> questionBankMapper;

    @Resource
    private CategoryMapper<Category, CategoryQuery> categoryMapper;

    @Resource
    private QuestionInfoMapper<QuestionInfo, QuestionInfoQuery> questionInfoMapper;

    @Resource
    private ExamQuestionMapper<ExamQuestion, ExamQuestionQuery> examQuestionMapper;

    @Override
    public Integer updateQuestionBankyByBankId(QuestionBank bean, Integer bankId) {
        return this.questionBankMapper.updateByBankId(bean, bankId);
    }

    @Override
    public Integer deleteQuestionBankByBankId(Integer bankId) {
        CategoryQuery categoryQuery = new CategoryQuery();
        categoryQuery.setBankId(bankId);
        Integer count = categoryMapper.selectCount(categoryQuery);
        if (count > 0) {
            throw new BusinessException("题库下已有分类！");
        }
        return this.questionBankMapper.deleteByBankId(bankId);
    }

    @Override
    public List<QuestionBank> loadAllCategoryByType(Integer type) {
       QuestionBankQuery questionBankQuery = new QuestionBankQuery();
       questionBankQuery.setType(type);
       return findListByParam(questionBankQuery);
    }

    /**
     * 根据条件查询列表
     */
    @Override
    public List<QuestionBank> findListByParam(QuestionBankQuery param) {
        return this.questionBankMapper.selectList(param);
    }

    /**
     * 根据条件查询列表
     */
    @Override
    public Integer findCountByParam(QuestionBankQuery param) {
        return this.questionBankMapper.selectCount(param);
    }

    /**
     * 分页查询方法
     */
    @Override
    public PaginationResultVO<QuestionBank> findListByPage(QuestionBankQuery param) {
        int count = this.findCountByParam(param);
        int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

        SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
        param.setSimplePage(page);
        List<QuestionBank> list = this.findListByParam(param);
        PaginationResultVO<QuestionBank> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
        return result;
    }

    /**
     * 新增
     */
    @Override
    public Integer add(QuestionBank bean) {
        return this.questionBankMapper.insert(bean);
    }

    /**
     * 批量新增
     */
    @Override
    public Integer addBatch(List<QuestionBank> listBean) {
        if (listBean == null || listBean.isEmpty()) {
            return 0;
        }
        return this.questionBankMapper.insertBatch(listBean);
    }

    /**
     * 批量新增或者修改
     */
    @Override
    public Integer addOrUpdateBatch(List<QuestionBank> listBean) {
        if (listBean == null || listBean.isEmpty()) {
            return 0;
        }
        return this.questionBankMapper.insertOrUpdateBatch(listBean);
    }

    /**
     * 多条件更新
     */
    @Override
    public Integer updateByParam(QuestionBank bean, QuestionBankQuery param) {
        StringTools.checkParam(param);
        return this.questionBankMapper.updateByParam(bean, param);
    }

    /**
     * 多条件删除
     */
    @Override
    public Integer deleteByParam(QuestionBankQuery param) {
        StringTools.checkParam(param);
        return this.questionBankMapper.deleteByParam(param);
    }

    /**
     * 根据CategoryId获取对象
     */
    @Override
    public QuestionBank getQuestionBankByCategoryId(Integer categoryId) {
        return this.questionBankMapper.selectByBankId(categoryId);
    }




    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveQuestionBank(QuestionBank questionBank) {
        if (null == questionBank.getBankId()) {
            QuestionBankQuery questionBankQuery = new QuestionBankQuery();
            Integer count = this.questionBankMapper.selectCount(questionBankQuery);
            questionBankQuery.setSort(count + 1);
            this.questionBankMapper.insert(questionBank);
        } else {
            this.questionBankMapper.updateByBankId(questionBank, questionBank.getBankId());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void changeSort(String bankIds) {
        String[] bankIdArray = bankIds.split(",");
        Integer index = 1;
        for (String bankIdStr : bankIdArray) {
            Integer bankId = Integer.parseInt(bankIdStr);
            QuestionBank questionBank = new QuestionBank();
            questionBank.setSort(index);
            questionBankMapper.updateByBankId(questionBank, bankId);
            index++;
        }
    }
}
